home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / CUJ9209.ARJ / 1009084A < prev    next >
Text File  |  1992-07-14  |  7KB  |  326 lines

  1. dir
  2.  
  3. /*
  4.    adaline.c
  5.  
  6.    Dwayne Phillips
  7.    February 1992
  8.  
  9.    The functions in this file implement the adaptive
  10.    linear element - Adaline - and the alpha-LMS
  11.    learning law.
  12.  
  13.    Contents:
  14.    calculate_net
  15.    calculate_output
  16.    display_inputs
  17.    display_weights
  18.    get_straight_input_from_user
  19.    get_straight_input_vectors
  20.    get_target_from_user
  21.    initialize_weights
  22.    process_new_case
  23.    train_the_adaline
  24.    train_weights
  25.  
  26. */
  27.  
  28. #include <stdio.h>
  29.  
  30.  
  31.  
  32. /*
  33.    long get_straight_input_vectors(inputs, x, N)
  34.  
  35.    This function gets an input vector from the user
  36.    via interaction using the screen and the keyboard.
  37. */
  38.  
  39.  
  40. long get_straight_input_vectors(inputs, x, N)
  41.    FILE  *inputs;
  42.    long  x[], N;
  43. {
  44.    char  string[80];
  45.    int   i, s;
  46.    long  target;
  47.  
  48.    printf("\nEnter number of input vectors >>");
  49.    gets(string);
  50.    s = atoi(string);
  51.    for(i=0; i<s; i++){
  52.       printf("\n\tInput vector %d", i);
  53.       get_straight_input_from_user(x, N);
  54.       display_inputs(x, N);
  55.       target = get_target_from_user();
  56.       x[N+1] = target;
  57.       fwrite(x, (N+2)*sizeof(long), 1, inputs);
  58.    }  /* ends loop over i */
  59.    fclose(inputs);
  60.  
  61. }  /* ends get_straight_input_vectors */
  62.  
  63.  
  64.  
  65.  
  66. /*
  67.    long get_straight_input_from_user(x, N)
  68.  
  69.    This function gets long from the
  70.    user via simple interaction using the screen
  71.    and the keyboard.
  72. */
  73.  
  74. long get_straight_input_from_user(x, N)
  75.    long x[], N;
  76. {
  77.    char  string[80];
  78.    int   a;
  79.    int   i;
  80.    long  s;
  81.  
  82.    for(i=0; i<N+1; i++) x[i] = -1;
  83.    x[0] = 1;
  84.  
  85.    for(i=1; i<N+1; i++){
  86.       printf("\nEnter input %d >>", i);
  87.       gets(string);
  88.       a    = atoi(string);
  89.       x[i] = a;
  90.    }
  91. }  /* ends get_straight_input_from_user */
  92.  
  93.  
  94.  
  95.  
  96. /*
  97.    long get_target_from_user()
  98.  
  99.    This function gets a long from the user
  100.    via the screen and keyboard.
  101. */
  102.  
  103. long get_target_from_user()
  104. {
  105.    char  string[80];
  106.    long  s;
  107.    printf("\nEnter the target >>");
  108.    gets(string);
  109.    s = atoi(string);
  110.    return(s);
  111. }  /* ends get_target_from_user */
  112.  
  113.  
  114.  
  115. /*
  116.    void display_weights(w, N)
  117.  
  118.    This function displays the weight vector
  119.    on the screen.
  120. */
  121.  
  122.  
  123. long display_weights(w, N)
  124.    long w[], N;
  125. {
  126.    int i;
  127.    for(i=0; i<N+1; i++){
  128.       if(i%15 == 0) printf("\n  ");
  129.       printf("%10ld", w[i]);
  130.    }
  131. }  /* ends display_weights */
  132.  
  133.  
  134.  
  135. /*
  136.    void display_inputs(x, N)
  137.  
  138.    This function displays the input vector
  139.    on the screen.
  140. */
  141.  
  142.  
  143. long display_inputs(x, N)
  144.    long x[], N;
  145. {
  146.    int i;
  147.    for(i=0; i<N+1; i++){
  148.       if(i%15 == 0) printf("\n  ");
  149.       printf("%6ld", x[i]);
  150.    }
  151. }  /* ends display_inputs */
  152.  
  153.  
  154.  
  155. /*
  156.    long train_the_adaline(inputs, weights, x, w, N)
  157.  
  158.    This function trains an Adaline.  It loops around
  159.    and around through all of the input vectors.  If
  160.    the Adaline produces the wrong answer for a vector,
  161.    this function calls the train_weights function which
  162.    trains the weights for that Adaline.  If the Adaline
  163.    produces the correct result for all of the input
  164.    vectors, then training is completed.  If one of the
  165.    vectors produces an incorrect result from the Adaline,
  166.    then the training loop repeats for all of the vectors.
  167.    If after too many attempts, the Adaline still produces
  168.    an incorrect answer, then you quit.
  169. */
  170.  
  171. long train_the_adaline(inputs, weights, x, w, N)
  172.    FILE  *inputs, *weights;
  173.    long  N, x[], w[];
  174. {
  175.    char  string[80];
  176.    int   i, s;
  177.    int   adjusting = 1,
  178.          counter   = 0;
  179.    long  net, output, target;
  180.    float eta = 0.5;
  181.  
  182.    initialize_weights(w, N);
  183.    printf("\nEnter number of input vectors >>");
  184.    gets(string);
  185.    s = atoi(string);
  186.    while(adjusting){
  187.       counter++;
  188.       adjusting = 0;
  189.       fseek(inputs, 0L, SEEK_SET);
  190.       for(i=0; i<s; i++){
  191.          fread(x, (N+2)*sizeof(long), 1, inputs);
  192.          display_inputs(x, N);
  193.          display_weights(w, N);
  194.          calculate_net(&net, N, w, x);
  195.          output = calculate_output(net);
  196.          target = x[N+1];
  197.          printf("\nnet=%ld  output=%ld  target=%ld", 
  198.                   net, output, target);
  199.  
  200.          if(output != target){
  201.             printf("\noutput does not equal target so train");
  202.             train_weights(target, net, eta, w, x, N);
  203.             display_weights(w, N);
  204.             adjusting = 1;
  205.          } /* ends if output != target */
  206.  
  207.       }  /* ends loop over i */
  208.       if(counter > 20){
  209.          printf("\n\nTOO MUCH TRAINING - quiting");
  210.          adjusting = 0;
  211.       }
  212.    }  /* ends while adjusting */
  213.  
  214.    printf("\n\nwent through the training cycle %d times",counter);
  215.    fclose(inputs);
  216.    fwrite(w, (N+1)*sizeof(long), 1, weights);
  217.    fclose(weights);
  218.  
  219.  
  220. }  /* ends train_the_adaline */
  221.  
  222.  
  223.  
  224.  
  225. /*
  226.    void initialize_weights(w, N)
  227.  
  228.    This function initializes the members of the
  229.    weights vector w.  Set the to 0 10 20 30 0 ...
  230. */
  231.  
  232. long initialize_weights(w, N)
  233.    long  w[], N;
  234. {
  235.    int i;
  236.    for(i=0; i<N+1; i++){
  237.       w[i] = ((i+1)%4) * 10;
  238.    }
  239. }  /* ends initialize_weights */
  240.  
  241.  
  242.  
  243.  
  244. /*
  245.    long calculate_net(net, N, w, x)
  246.  
  247.    This function calculates the net
  248.    net = vector product of x * w
  249. */
  250.  
  251. long calculate_net(net, N, w, x)
  252.    long *net, N, w[], x[];
  253. {
  254.    long   i;
  255.    *net = 0;
  256.    for(i=0; i<N+1; i++){
  257.       *net = *net + w[i]*x[i];
  258.    }
  259. }  /* ends calculate_net */
  260.  
  261.  
  262.  
  263.  
  264. /*
  265.    long calculate_output(net)
  266.  
  267.    This function set the output value
  268.    based on the net.
  269.    output = 1 if net >= 0
  270.    output = 0 if net < 0
  271. */
  272. long calculate_output(net)
  273.    long net;
  274. {
  275.    long result = 1;
  276.    if(net < 0) result = -1;
  277.    return(result);
  278. }  /* ends calculate_output */
  279.  
  280.  
  281.  
  282.  
  283. /*
  284.    long train_weights(target, net, eta, w, x, N)
  285.  
  286.    This function adjusts the weights in the weight
  287.    vector w.  It uses the alpha LMS algorithm.
  288. */
  289.  
  290. long train_weights(target, net, eta, w, x, N)
  291.    long target, net, w[], x[], N;
  292.    float eta;
  293. {
  294.    long delta_w, i;
  295.    for(i=0; i<N+1; i++){
  296.       delta_w = eta*x[i]*(target-net);
  297.       w[i]    = w[i] + delta_w;
  298.    }
  299. }  /* ends train_weights */
  300.  
  301.  
  302.  
  303.  
  304. /*
  305.    long process_new_case(weights, x, w, N)
  306.  
  307.    This function process a case for a new input.
  308.    It is the working mode for the neural network.
  309. */
  310.  
  311. long process_new_case(weights, x, w, N)
  312.    FILE   *weights;
  313.    long  N, x[], w[];
  314. {
  315.    long net, output;
  316.  
  317.    fread(w, (N+1)*sizeof(long), 1, weights);
  318.    fclose(weights);
  319.    get_straight_input_from_user(x, N);
  320.    display_inputs(x, N);
  321.    display_weights(w, N);
  322.    calculate_net(&net, N, w, x);
  323.    output = calculate_output(net);
  324.    printf("\nnet=%ld  output=%ld  ", net, output );
  325. }  /* ends process_new_case */
  326.